<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="keywords" content=""/>
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/urlutil.js"></script>
    <script src="js/marked.min.js"></script>
    <script src="js/loginutil.js"></script>
</head>
<body>
<nav id="navbar" class="navbar has-shadow">
    <div class="container">
        <div class="navbar-brand">
            <div class="navbar-burger burger" data-target="navMenu">
                <span></span>
                <span></span>
                <span></span>
            </div>
        </div>
        <div id="navMenu" class="navbar-menu">
            <div class="navbar-start">
                <a class="navbar-item" href="index.html">主页</a>
                <a class="navbar-item" href="about.html">关于</a>
            </div>
            <div class="navbar-end">
                <div id="login_div" style="display: none;" class="navbar-item">
                    <a id="category_href" style="display: none" class="button is-small is-primary" href="category.html">分类管理</a>&nbsp;&nbsp;&nbsp;
                    <a class="button is-small is-primary" href="add-article.html">写文章</a>&nbsp;&nbsp;&nbsp;
                    <a id="login_username" href="user-articlelist.html"></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:logout();">退出</a>
                </div>
                <div id="unlogin_div" class="navbar-item">
                    <a class="button is-small is-primary" href="register.html">注册</a>&nbsp;&nbsp;&nbsp
                    <a class="button is-small" href="login.html">登录</a>
                </div>
            </div>
        </div>
    </div>
</nav>
<script>
    jQuery(function () {
        var login_username = window.localStorage.getItem("username");
        if (login_username == null) {
            // 未登录
            jQuery("#unlogin_div").show();
        } else { // 已经登录
            jQuery("#unlogin_div").hide();
            jQuery("#login_username").html(login_username);
            jQuery("#login_username").attr("href", "user-articlelist.html?uid=" +
                window.localStorage.getItem("uid"));
            jQuery("#login_div").show();
            // 管理员权限判断
            if (window.localStorage.getItem("manager") == 1) {
                jQuery("#category_href").show();
            }
        }
    });
</script>
<div class="container margin-top">
    <div class="columns">
        <div class="column is-8 is-offset-2">
            <div class="box markdown-body">
                <h1 id="title"></h1>
                <div>
                    作者：<a href="" id="username"></a>
                    <div class="is-pulled-right" id="art_edit" style="display: none;">
                        <a id="edit_art_href" class="button is-small is-primary" href="">编辑</a>
                        <a id="del_art_href" class="button is-small is-danger" href="">删除</a>
                    </div>
                </div>
                <p id="content">

                </p>
                <p>
                    <a href="javascript:void(0);" class="tag is-primary" id="category"></a>
                </p>
            </div>
            <section class="margin-top box">
                <div class="media">
                    <div class="media-content">
                        <div class="field">
                            <p class="control">
                                <textarea id="comment_content" class="textarea" placeholder="发表评论…"></textarea>
                            </p>
                        </div>
                        <button class="button is-info is-pulled-right" onclick="doAddComment()">Submit</button>
                    </div>
                </div>
                <hr>
                <div id="commentlist">
                </div>
            </section>
        </div>
    </div>
</div>
<script>
    // 从当前 url 中获取 aid
    var aid = getUrlParam("aid");
    var isowner_flag = false;

    // 文章删除功能
    function doArtDel(aid) {
        if (confirm("确认删除？")) {
            jQuery.ajax({
                url: req_base_url + "art/delbyaid",
                method: "POST",
                headers: {
                    "Authorization": window.localStorage.getItem(token_key)
                },
                data: {
                    "aid": aid
                },
                success: function (res) {
                    // 将执行的结果展示给用户
                    if (res != null && res.code == 200 &&
                        res.data) {
                        alert("恭喜：删除成功！");
                        location.href = "user-articlelist.html?uid=" +
                            window.localStorage.getItem("uid");
                    } else {
                        // 失败
                        alert("抱歉：删除失败，请重试！" + res.msg);
                    }
                }
            });
        }
    }

    // 初始化文章详情信息
    function initArt() {
        // 参数效验
        if (aid == null || aid <= 0) {
            alert("非法访问，请重试！");
            return false;
        }
        jQuery.ajax({
            url: req_base_url + "art/getbyid",
            method: "GET",
            data: {
                "aid": aid
            },
            success: function (res) {
                // 将执行的结果展示给用户
                if (res != null && res.code == 200 &&
                    res.data != null) {
                    // 查询成功
                    jQuery("#title").html(res.data.title);
                    jQuery("#username").html(res.data.username);
                    jQuery("#username").attr("href", "user-articlelist.html?uid=" +
                        res.data.uid);
                    jQuery("#category").html(res.data.category);
                    jQuery("#content").html(marked(res.data.content));
                    jQuery("#edit_art_href").attr("href", "edit-article.html?aid=" +
                        res.data.aid);
                    jQuery("#del_art_href").attr("href", "javascript:doArtDel(" +
                        res.data.aid + ");");
                    // 进行文章归属的判断
                    var uid = window.localStorage.getItem("uid");
                    if (uid != null && uid > 0) {
                        if (uid == res.data.uid) {
                            isowner_flag = true;
                            // 显示【操作按钮=修改||删除】
                            jQuery("#art_edit").show();
                        }
                    }
                    // 2.评论列表展示
                    initCommentList();
                } else {
                    // 失败
                    alert("抱歉：页面数据加载失败！" + res.msg);
                }
            }
        });
    }

    // 初始化评论列表
    function initCommentList() {
        jQuery.ajax({
            url: req_base_url + "comment/listbyaid",
            method: "GET",
            data: {
                "aid": aid
            },
            success: function (res) {
                // 将执行的结果展示给用户
                if (res != null && res.code == 200 &&
                    res.data != null) {
                    // 成功
                    var commentHtml = "";
                    for (var i = 0; i < res.data.length; i++) {
                        var comment = res.data[i];
                        commentHtml += '<article class="media comment">';
                        commentHtml += '<figure class="media-left">';
                        commentHtml += '<p class="image is-24x24">';
                        commentHtml += '<img src="image/cat.png">';
                        commentHtml += '</p>';
                        commentHtml += '</figure>';
                        commentHtml += '<div class="media-content">';
                        commentHtml += '<div class="content">';
                        commentHtml += '<p>';
                        commentHtml += '<a href="user-articlelist.html?uid=' + comment.uid + '">';
                        commentHtml += '<strong>' + comment.username + '</strong>';
                        commentHtml += '</a>';
                        commentHtml += '<small></small>';
                        commentHtml += '<br>';
                        commentHtml += comment.content;
                        commentHtml += '</p>';
                        commentHtml += '</div>';
                        commentHtml += '<nav></nav>';
                        commentHtml += '</div>';
                        commentHtml += '<div class="media-right is-invisible">';
                        commentHtml += '<button  class="button is-small is-primary">回复</button>';
                        commentHtml += '&nbsp;&nbsp;<a name="isowner"  href="javascript:doDelComment(' + comment.cid
                            + ');" class="button is-small">删除</a>';
                        commentHtml += '</div>';
                        commentHtml += '</article>';
                    }
                    jQuery("#commentlist").html(commentHtml);
                   
                } else {
                    // 失败
                    alert("抱歉：页面数据加载失败！" + res.msg);
                }
            }
        });
    }

    // 初始化页面数据
    function initPage() {
        // 1.文章详情展示
        initArt();

    }

    initPage();

    // 添加评论
    function doAddComment() {
        // 非空效验
        var comment_content = jQuery("#comment_content");
        if (comment_content.val().trim() == "") {
            alert("请先输入评论内容！");
            comment_content.focus();
            return false;
        }
        jQuery.ajax({
            url: req_base_url + "comment/add",
            method: "POST",
            headers: {
                "Authorization": window.localStorage.getItem(token_key)
            },
            data: {
                "aid": aid,
                "content": comment_content.val()
            },
            success: function (res) {
                // 将执行的结果展示给用户
                if (res != null && res.code == 200 && res.data) {
                    alert("恭喜：添加成功！");
                    location.href = location.href;
                } else {
                    // 失败
                    alert("抱歉：添加失败，请重试！" + res.msg);
                }
            }
        });
    }
    // 评论删除
    function doDelComment(cid){
        if(confirm("是否确认删除？")){
            jQuery.ajax({
                url: req_base_url + "comment/del",
                method: "POST",
                headers: {
                    "Authorization": window.localStorage.getItem(token_key)
                },
                data: {
                    "cid": cid
                },
                success: function (res) {
                    if (res != null && res.code == 200 && res.data) {
                        alert("恭喜：删除成功！");
                        location.href = location.href;
                    } else {
                        // 失败
                        alert("抱歉：删除失败，请重试！" + res.msg);
                    }
                }
            });
        }
    }
</script>
</body>
</html>
